package ore.example.pokemoninfo.utils;

import com.pixelmonmod.pixelmon.api.pokemon.Pokemon;
import com.pixelmonmod.pixelmon.api.pokemon.stats.evolution.Evolution;
import com.pixelmonmod.pixelmon.api.pokemon.stats.evolution.conditions.EvoCondition;
import com.pixelmonmod.pixelmon.api.pokemon.stats.evolution.types.TradeEvolution;
import com.pixelmonmod.pixelmon.api.pokemon.species.Species;
import com.pixelmonmod.pixelmon.api.storage.PartyStorage;
import com.pixelmonmod.pixelmon.api.storage.StorageProxy;
import org.bukkit.entity.Player;

import java.util.Iterator;
import java.util.List;

public class TradeEvolutionHandler {

    /**
     * 尝试让玩家背包中的指定宝可梦进化
     * 忽略交易条件，但保留其他条件（如携带物、等级等）
     */
    public static void tryEvolve(Player player, Species targetSpecies) {
        PartyStorage storage = StorageProxy.getPartyNow(player.getUniqueId());

        for (Pokemon pokemon : storage.getAll()) {
            if (pokemon == null) continue;
            if (!pokemon.getSpecies().equals(targetSpecies)) continue;

            List<Evolution> evolutions = pokemon.getForm().getEvolutions();
            for (Evolution evo : evolutions) {
                if (!(evo instanceof TradeEvolution tradeEvo)) continue;

                boolean otherConditionsMet = true;
                for (EvoCondition cond : evo.conditions()) {
                    if (cond.getClass().getSimpleName().equals("TradeCondition")) continue; // 忽略交易条件
                    if (!cond.passes(pokemon)) { // passes(Pokemon) 返回是否满足条件
                        otherConditionsMet = false;
                        break;
                    }
                }

                if (!otherConditionsMet) {
                    player.sendMessage("§c宝可梦未满足其他进化条件（携带物、等级等）");
                    return;
                }

                boolean success = evo.doEvolution(pokemon, "custom_trade_gui");
                if (success) player.sendMessage("§a进化成功！");
                else player.sendMessage("§c进化失败");
                return;
            }

        }

        player.sendMessage("§c你背包中没有可进化的 " + targetSpecies.getName() + "。");
    }
}
